<template>
  <j-modal
    :title="title"
    :width="1200"
    :visible="visible"
    :maskClosable="false"
    
    :fullscreen="true"
    @ok="handleOk"
    @cancel="handleCancel"
    :destroyOnClose="true"
    cancelText="关闭"
    :footer="null"
  >
    <template slot="footer"> </template>
    <add ref="modal" :entryId ="model.id" :disabled="disableSubmit"></add>
  </j-modal>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import Add from '@/views/exTenderFile/Add'

export default {
  name: 'AddModal',
  components: {
    Add,
  },
  props: {
    projectId: {
      default: '',
    },
    sectionId: {
      default: '',
    },
  },
  data() {
    return {
      title: '招标文件-编辑',
      visible: false,
      disableSubmit:false,
      model: {},
    }
  },
  watch: {
    projectId(newVal, oldVal) {
      this.condition = '{"project_id":"' + newVal + '","section_id":"' + this.sectionId + '"}'
    },
  },
  created() {},
  computed: {
    disabled() {
      return this.model.id ? true : false
    },
  },
  methods: {
    add() {
      this.edit({})
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    close() {
     /* if (this.$refs.modal.chapterRef.selectedRowKeys.length == 0) {
        this.$refs.modal.noticeContent = ''
      }*/

      this.$emit('close')
      this.visible = false
      this.disableSubmit = false;
      this.$refs.form.resetFields()

    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate((valid) => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          that.model.projectId = that.projectId
          that.model.sectionId = that.sectionId
          httpAction(httpurl, this.model, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                // close的时候清空了表单的值 导致model为空 修改值在列表页没有变 此处需要复制一下model
                that.submitSuccess({ ...this.model })
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
            })
        } else {
          return false
        }
      })
    },
    handleCancel() {
      this.close()
    },
    submitSuccess(formData) {
      if (!formData.id) {
        let treeData = this.$refs.treeSelect.getCurrTreeData()
        this.expandedRowKeys = []
        this.getExpandKeysByPid(formData[this.pidField], treeData, treeData)
        if (formData.pid && this.expandedRowKeys.length == 0) {
          this.expandedRowKeys = this.subExpandedKeys
        }
        this.$emit('ok', formData, this.expandedRowKeys.reverse())
      } else {
        this.$emit('ok', formData)
      }
    },
  },
}
</script>
